package com.xinxing.city.recovery.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.Tools;
import com.xinxing.city.recovery.entitys.MenuBean;
import com.xinxing.city.recovery.utils.MyDialog;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import java.lang.reflect.Type;
import java.util.List;

public class SetAbilityConActivity extends AppCompatActivity {
    private String per;
    private int position;
    private MenuBean menuBean;
    private List<MenuBean> menuList;
    private Gson gson;
    private String abId;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_ability_con);
        gson = new Gson();
        //本地变量

        //参数
        Intent intent = getIntent();
        abId = intent.getStringExtra("id");

        String menuList_c = (String) SharedPreferencesUtils.getParam(this,"menu_list","");
        if (menuList_c.equals("")) finish();
        Type menuListType = new TypeToken<List<MenuBean>>() {}.getType();
        menuList = gson.fromJson(menuList_c,menuListType);

        //获取当前本地变量
        per = abId.substring(8,abId.length());
        for (int i = 0;i < menuList.size();i++) {
            if (menuList.get(i).getCode().equals(per)) {
                position = i;
                menuBean = menuList.get(i);
            }
        }
        if(menuBean==null){
            MyDialog myDialog = MyDialog.newInstance("提示","此功能目前未开放","关闭","");
            myDialog.show(getSupportFragmentManager(),"MyDialog");
            myDialog.setMyDialogListener(new MyDialog.MyDialogListener() {
                @Override
                public void onPositiveClick() {
                    SetAbilityConActivity.this.finish();
                }

                @Override
                public void onNegativeClick() {

                }
            });
        }else{
            //页面初始化
            initPage();
        }

    }
    //页面初始化
    private void initPage() {

        LinearLayout pageSource = findViewById(getResources().getIdentifier(abId,"id",getPackageName()));
        pageSource.setVisibility(View.VISIBLE);

        //标题
        TextView title = findViewById(R.id.con_title);
        //底部标题
        EditText btext1 = findViewById(R.id.con_btext1);

        if (menuBean.getTitle()!=null&&!menuBean.getTitle().equals(""))
            btext1.setText(menuBean.getTitle());
        //弹窗自动取消文字描述
        TextView tcAutoOutText1 = findViewById(R.id.tcAutoOutText1);
        //是否启用
        Switch is_show = findViewById(R.id.is_show);
        String is_show_c = menuBean.getIs_show();
        if(is_show_c.equals("1")) {
            is_show.setChecked(true);
        } else {
            is_show.setChecked(false);
        }
        //自动退出时间
        EditText auto_out_time = findViewById(R.id.auto_out_time);
        String auto_out_time_c = menuBean.getAuto_out_time();
        if (!auto_out_time_c.equals("")) auto_out_time.setText(auto_out_time_c);

        //同意匹配底部和顶部文案
        if (!abId.equals("ability_sz")) {

            //顶部文案
            EditText top_text = findViewById(getResources().getIdentifier(per+"_top_text","id",getPackageName()));
            String top_text_c = menuBean.getTop_text();
            if (!top_text_c.equals("")) top_text.setText(top_text_c);

            //是否进行ai播报
            Switch top_ai = findViewById(getResources().getIdentifier(per+"_top_ai","id",getPackageName()));
            String top_ai_c = menuBean.getTop_ai();
            if(top_ai_c.equals("1")) {
                top_ai.setChecked(true);
            } else {
                top_ai.setChecked(false);
            }

            //底部文案
            EditText bot_text = findViewById(getResources().getIdentifier(per+"_bot_text","id",getPackageName()));
            String bot_text_c = menuBean.getBot_text();
            if (!bot_text_c.equals("")) bot_text.setText(bot_text_c);

            //是否进行ai播报
            Switch bot_ai = findViewById(getResources().getIdentifier(per+"_bot_ai","id",getPackageName()));
            String bot_ai_c = menuBean.getBot_ai();
            if(bot_ai_c.equals("1")) {
                bot_ai.setChecked(true);
            } else {
                bot_ai.setChecked(false);
            }
        }


        if (abId.equals("ability_rlsbjt")) { //人脸识别交投
            title.setText("人脸识别交投");
            tcAutoOutText1.setText("弹窗页，几秒后未识别成功自动退出");

            //失败文案
            EditText fail_text = findViewById(R.id.rlsbjt_fail_text);
            String fail_text_c = menuBean.getFail_text();
            if (!fail_text_c.equals("")) fail_text.setText(fail_text_c);

            //是否进行ai播报
            Switch fail_ai = findViewById(R.id.rlsbjt_fail_ai);
            String fail_ai_c = menuBean.getFail_ai();
            if(fail_ai_c.equals("1")) {
                fail_ai.setChecked(true);
            } else {
                fail_ai.setChecked(false);
            }

        } else if(abId.equals("ability_sjhjt")) { //手机号交投
            title.setText("手机号交投");
            tcAutoOutText1.setText("弹窗页，几秒后未去交投自动退出");

        } else if(abId.equals("ability_smqh")) { //扫码取货
            title.setText("扫码取货");
            tcAutoOutText1.setText("弹窗页，几秒后未扫码自动退出");

        } else if(abId.equals("ability_smqj")) { //扫码清洁
            title.setText("扫码清洁");
            tcAutoOutText1.setText("弹窗页，几秒后未扫码自动退出");

        } else if(abId.equals("ability_sz")) { //设置
            title.setText("设置");
            tcAutoOutText1.setText("弹窗页，几秒后未扫码自动退出");
        } else {    //扫码交投
            title.setText("扫码交投");
            tcAutoOutText1.setText("弹窗页，几秒后未扫码自动退出");
        }
    }

    //数据提交
    public void saveSub(View view) {

        //是否启用
        Switch is_show = findViewById(R.id.is_show);
        //标题
        EditText title = findViewById(R.id.con_btext1);
        if(title.getText().toString().equals("")) {
            Tools.logTips("显示文案不能为空！",getSupportFragmentManager());
            return ;
        }
        //自动退出时间
        EditText auto_out_time = findViewById(R.id.auto_out_time);
        if(Integer.parseInt(auto_out_time.getText().toString()) < 10) {
            Tools.logTips("自动退出时间不能小于10秒！",getSupportFragmentManager());
            return ;
        }

        //设置状态
        if (is_show.isChecked()) {
            menuBean.setIs_show("1");
        } else {
            menuBean.setIs_show("2");
        }
        menuBean.setTitle(title.getText().toString());
        menuBean.setAuto_out_time(auto_out_time.getText().toString());

        if (!per.equals("sz")) {
            //顶部文案
            EditText top_text = findViewById(getResources().getIdentifier(per+"_top_text","id",getPackageName()));
            menuBean.setTop_text(top_text.getText().toString());

            //是否进行ai播报
            Switch top_ai = findViewById(getResources().getIdentifier(per+"_top_ai","id",getPackageName()));
            if(top_ai.isChecked()) {
                menuBean.setTop_ai("1");
            } else {
                menuBean.setTop_ai("2");
            }

            //底部文案
            EditText bot_text = findViewById(getResources().getIdentifier(per+"_bot_text","id",getPackageName()));
            menuBean.setBot_text(bot_text.getText().toString());

            //是否进行ai播报
            Switch bot_ai = findViewById(getResources().getIdentifier(per+"_bot_ai","id",getPackageName()));
            if(bot_ai.isChecked()) {
                menuBean.setBot_ai("1");
            } else {
                menuBean.setBot_ai("2");
            }
        }


        if (abId.equals("ability_rlsbjt")) { //人脸识别交投

            //失败文案
            EditText fail_text = findViewById(R.id.rlsbjt_fail_text);
            menuBean.setFail_text(fail_text.getText().toString());

            //是否进行ai播报
            Switch fail_ai = findViewById(R.id.rlsbjt_fail_ai);
            if(fail_ai.isChecked()) {
                menuBean.setFail_ai("1");
            } else {
                menuBean.setFail_ai("2");
            }

        }

        menuList.set(position,menuBean);
        SharedPreferencesUtils.setParam(this,"menu_list",gson.toJson(menuList));
        SetAbilityConActivity.this.finish();
    }

}
